
// Example 2. Application Using C and mcBLAS: 0-based indexing
// example2.cpp
//-----------------------------------------------------------
#include <math.h>
#include <mc_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include "mcblas.h"
#define M 6
#define N 5
#define IDX2C(i, j, ld) (((j) * (ld)) + (i))

static __inline__ void
    modify(mcblasHandle_t handle, float* m, int ldm, int n, int p, int q, float alpha, float beta) {
    mcblasSscal(handle, n - q, &alpha, &m[IDX2C(p, q, ldm)], ldm);
    mcblasSscal(handle, ldm - p, &beta, &m[IDX2C(p, q, ldm)], 1);
}

int main(void) {
    int mcStat;
    mcblasStatus_t stat;
    mcblasHandle_t handle;
    int i, j;
    float* devPtrA;
    float* a = 0;
    a = (float*)malloc(M * N * sizeof(*a));
    if (!a) {
        printf("host memory allocation failed");
        return EXIT_FAILURE;
    }
    for (j = 0; j < N; j++) {
        for (i = 0; i < M; i++) {
            a[IDX2C(i, j, M)] = (float)(i * N + j + 1);
        }
    }
    mcStat = mcMalloc((void**)&devPtrA, M * N * sizeof(*a));
    if (mcStat != 0) {
        printf("device memory allocation failed");
        return EXIT_FAILURE;
    }
    stat = mcblasCreate(&handle);
    if (stat != MCBLAS_STATUS_SUCCESS) {
        printf("MCBLAS initialization failed\n");
        return EXIT_FAILURE;
    }
    stat = mcblasSetMatrix(M, N, sizeof(*a), a, M, devPtrA, M);
    if (stat != MCBLAS_STATUS_SUCCESS) {
        printf("data download failed");
        mcFree(devPtrA);
        mcblasDestroy(handle);
        return EXIT_FAILURE;
    }
    modify(handle, devPtrA, M, N, 1, 2, 16.0f, 12.0f);
    stat = mcblasGetMatrix(M, N, sizeof(*a), devPtrA, M, a, M);
    if (stat != MCBLAS_STATUS_SUCCESS) {
        printf("data upload failed");
        mcFree(devPtrA);
        mcblasDestroy(handle);
        return EXIT_FAILURE;
    }
    mcFree(devPtrA);
    mcblasDestroy(handle);
    for (j = 0; j < N; j++) {
        for (i = 0; i < M; i++) {
            printf("%7.0f", a[IDX2C(i, j, M)]);
        }
        printf("\n");
    }
    free(a);
    return EXIT_SUCCESS;
}